<?php


namespace App\Logic\Purchase\Form;

use App\Core\BaseListForm;
use App\Core\Exception\DefaultException;
use App\Event\Event;
use App\Logic\Account\CashoutService;
use App\Logic\Member\MemberScoreService;
use App\Model\Account\CashoutModel;
use App\Model\Member\MemberModel;
use App\Model\Member\MemberScoreModel;
use App\Model\Purchase\PurchaseModel;
use App\Util\CommonHelper;
use App\Util\UserIdentityHelper;
use EasySwoole\ORM\DbManager;

class EditPurchaseForm extends BaseListForm
{
    public $id;
    public $goods_name;
    public $goods_img;
    public $goods_quantity;
    public $goods_price;
    public $order_state;
    public $order_time;
    public $shipped_time;
    public $receipt_time;
    /**
     * 校验规则 rules
     * @return array[]
     */
    public function rules()
    {
        return [
            'id'             => ['required', '订单ID', '订单ID错误'],
            'goods_quantity' => ['required|integer|min:1|max:999', '下单数量', '下单数量[1-999]'],
            'goods_price'    => ['required|numeric|min:0.01', '商品单价', '商品单价错误'],
            'goods_name'     => ['required', '商品名称', '商品名称错误'],
            'goods_img'      => ['required', '商品图片', '商品图片错误'],
            'order_state'    => ['required', '订单状态', '订单状态错误'],
            'order_time'     => ['required', '下单时间', '下单时间错误'],
            'shipped_time'   => ['required', '发货时间', '发货时间错误'],
            'receipt_time'   => ['required', '收货时间', '收货时间错误'],
        ];
    }
    public function validate()
    {
        //生成验证器，方法自动读取rules方法返回的值数据
        $validator = $this->createValidator();
        $validator->addColumn('order_state')->inArray([PurchaseModel::UNSHIPPING, PurchaseModel::SHIPPED, PurchaseModel::RECEIPT]);
//        $validator->addColumn('shipped_time')->timestampAfterDate($this->order_time);
//        $validator->addColumn('receipt_time')->timestampAfterDate($this->shipped_time);
        if (!($check = $validator->validate($this->getAttributes()))) {
            throw new DefaultException($validator->getError()->__toString(), 600);
        }
        if(strtotime($this->shipped_time) > strtotime($this->receipt_time)){
            throw new DefaultException('收货时间不能早于发货时间', 600);
        }
        if(strtotime($this->order_time) > strtotime($this->shipped_time)){
            throw new DefaultException('发货时间不能早于下单时间', 600);
        }

        return true;
    }

    public function edit()
    {
        $this->validate();
        //插入数据
        DbManager::getInstance()->startTransactionWithCount('write');
        try {
            //添加表
            $editData = [
                'goods_name'     => $this->goods_name,
                'goods_img'      => $this->goods_img,
                'goods_price'    => $this->goods_price,
                'goods_quantity' => $this->goods_quantity,
                'order_amount'   => bcmul($this->goods_price, $this->goods_quantity, 2),
                'order_state'    => $this->order_state,
                'order_time'     => $this->order_time,
                'shipped_time'   => $this->shipped_time,
                'receipt_time'   => $this->receipt_time,
            ];

            //插入数据库
            $res = PurchaseModel::updateData($editData, ['id' => $this->id]);
            $id  = $this->id;

            DbManager::getInstance()->commitWithCount('write');

            $returnData = ['id' => $id];

            $result = $this->success($returnData);
            return $result;
        } catch (\Throwable $e) {
            DbManager::getInstance()->rollbackWithCount('write');
            CommonHelper::logException($e, '下单错误');
            throw $e;
        }
    }

    /**
     * 订单编号 getOrderNo
     * @return string
     */
    public function getOrderId($time)
    {
        return 'OR' . date('YmdHis', $time) . commonHelper::getRandom('', 8);
    }

}
